package com.lanou.moving.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.lanou.moving.R;
import com.lanou.moving.adapter.NewSongAdapter;
import com.lanou.moving.base.BaseFragment;

import it.sephiroth.android.library.picasso.Picasso;

/**
 * Created by 梁树珍 on 15/11/3.
 */
public class NewSongSelection extends BaseFragment implements View.OnClickListener, ViewPager.OnPageChangeListener {
    private ViewPager language_viewPager;
    private TabLayout new_song_tablayout;
    private NewSongAdapter songAdapter;
    private Toolbar new_song_toobar;
    private ImageView new_song_img;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.new_song_selection, null);
        language_viewPager = (ViewPager) view.findViewById(R.id.language_viewPager);
        new_song_tablayout = (TabLayout) view.findViewById(R.id.new_song_tablayout);
        new_song_toobar = (Toolbar) view.findViewById(R.id.new_song_toobar);
        new_song_toobar.setNavigationOnClickListener(this);
        new_song_img = (ImageView) view.findViewById(R.id.new_song_img);
        getPic(new NewSongChinese().picUrl);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        init();

    }

    public void getPic(String url){
        Picasso.with(mContext).load(url).into(new_song_img);
    }
    private void init(){
        songAdapter = new NewSongAdapter(getFragmentManager());
        language_viewPager.setAdapter(songAdapter);
        new_song_tablayout.setupWithViewPager(language_viewPager);
        new_song_tablayout.setTabsFromPagerAdapter(songAdapter);
        new_song_tablayout.setSelectedTabIndicatorColor(getResources().getColor(R.color.resideMenu_bg));
        new_song_tablayout.setTabTextColors(0xffffffff, 0xffeef203);
        language_viewPager.addOnPageChangeListener(this);
    }

    @Override
    public void onClick(View v) {
        getFragmentManager().popBackStack();
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        switch (position){
            case 0:
                getPic(new NewSongChinese().picUrl);
                break;
            case 1:
                getPic(new NewSongWestern().picUrl);
                break;
            case 2:
                getPic(new NewSongKorean().picUrl);
                break;
            case 3:
                getPic(new NewSongJapanese().picUrl);
                break;
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
}
